package com.cskaoyan.java._1myexception;

/**
 * 如何自定义异常?
 *      1.运行时异常,自定义一个类,让它继承RuntimeException类
 *      2.编译时异常,自定义一个类,让它继承Exception类
 *
 * @since 17:32
 * @author wuguidong@cskaoyan.onaliyun.com
 */
public class Demo2 {
    public static void main(String[] args) {
        try {
            judgeScore(120);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (ScoreNumException e) {
            e.printStackTrace();
        }

        try {
            judge(100);
        } catch (ScoreNumCheckedException e) {
            e.printStackTrace();
        }
    }

    // 考试成绩必须在0-100分之间，如果有考试成绩不在这个范围之内，则认为成绩异常。
    public static void judgeScore(int score) {
        if (score < 0) {
            throw new IllegalArgumentException("你的成绩小于0,异常!");
        }
        if (score > 100) {
            throw new ScoreNumException("你的成绩大于100,异常!");
        }
    }

    public static void judge(int score) throws ScoreNumCheckedException {
        if (score > 0) {
            throw new ScoreNumCheckedException("你的成绩小于0,异常!");
        }

        if (score > 100) {
            throw new ScoreNumCheckedException("你的成绩大于100,异常!");
        }
    }
}

// 自定义运行时异常
class ScoreNumException extends RuntimeException {
    public ScoreNumException() {
    }

    public ScoreNumException(String message) {
        super(message);
    }
}

class ScoreNumCheckedException extends Exception {
    public ScoreNumCheckedException() {
    }

    public ScoreNumCheckedException(String message) {
        super(message);
    }
}